<template>
  <div id="detail">
    <detail-nav-bar class="detail-nav" @itemClick="titleClick" ref="navbar"/>
    <scroll class="content" ref="scroll" :probe-type="3" @scrollP="scrollPosition">
      <detail-swiper :top-images="topImages"/>
      <detail-base-info :goods="goods"/>
      <detail-shop-info :shop="shop"/>
      <detail-goods-info :detailGoodsInfo="detailGoodsInfo"
                         @detailImgLoad="detailImgLoad"/>
      <detail-goods-params :paramInfo="paramInfo" ref="params"/>
      <detail-comment-info :comment-info="commentInfo" ref="comment"/>
      <detail-recommend-info :recommendList="recommendList" ref="recommend"/>
    </scroll>

    <back-top @click.native="backClick" v-show="isShowBackTop"/>
    <detail-bottom-bar @addToCart="addToCart"/>
    <toast :is-show="isShowAlter" :alter-message="alterMessage"/>
  </div>
</template>

<script>
import Scroll from '@/components/common/scroll/Scroll'
import {getDetail, getRecommend, Goods, Shop, GoodsParam} from '@/network/detail'

import DetailNavBar from '@/views/detail/childComps/DetailNavBar'
import DetailSwiper from '@/views/detail/childComps/DetailSwiper'
import DetailBaseInfo from '@/views/detail/childComps/DetailBaseInfo'
import DetailShopInfo from '@/views/detail/childComps/DetailShopInfo'
import DetailGoodsInfo from '@/views/detail/childComps/DetailGoodsInfo'
import DetailGoodsParams from '@/views/detail/childComps/DetailGoodsParams'
import DetailCommentInfo from '@/views/detail/childComps/DetailCommentInfo'
import DetailRecommendInfo from '@/views/detail/childComps/DetailRecommendInfo'
import DetailBottomBar from '@/views/detail/childComps/DetailBottomBar'
import {backTopMixin} from '@/common/mixin'
import BackTop from '@/components/content/backtop/BackTop'
import Toast from '@/components/content/Toast/Toast'

export default {
  name: "Detail",
  components: {
    DetailNavBar,
    DetailSwiper,
    DetailBaseInfo,
    DetailShopInfo,
    DetailGoodsInfo,
    DetailGoodsParams,
    DetailCommentInfo,
    DetailRecommendInfo,
    DetailBottomBar,
    Scroll,
    BackTop,
    Toast
  },
  mixins: [backTopMixin],
  data() {
    return {
      iid: null,
      topImages: [],
      goods: {},
      shop: {},
      detailGoodsInfo: {},
      paramInfo: {},
      commentInfo: {},
      recommendList: [],
      themeTopYs: [],
      currentIndex: 0,
      isShowAlter: false,
      alterMessage: ''
    }
  },
  created() {
    // 1. 保存传入的iid
    this.iid = this.$route.params.iid

    // 2. 根据iid请求数据
    getDetail(this.iid).then(res => {
      // console.log(res)
      const data = res.result
      // 轮播图数据
      this.topImages = data.itemInfo.topImages

      // 商品信息
      this.goods = new Goods(data.itemInfo, data.columns, data.shopInfo.services)

      // 店铺信息
      this.shop = new Shop(data.shopInfo)

      // 商品信息
      this.detailGoodsInfo = data.detailInfo

      // 商品参数信息
      this.paramInfo = new GoodsParam(data.itemParams.info, data.itemParams.rule)

      // 评论信息（有的商品可能没有评论）
      if (data.rate.list) {
        this.commentInfo = data.rate.list[0];
      }
    })

    // 3. 推荐信息
    getRecommend().then(res => {
      this.recommendList = res.data.list
    })
  },
  updated() {
    this.$refs.scroll.refresh()
  },
  mounted() {
  },
  methods: {
    detailImgLoad() {
      this.$refs.scroll.refresh()

      // 图片加载完成后，获取顶部的偏移量
      this.themeTopYs = []
      this.themeTopYs.push(0)
      this.themeTopYs.push(this.$refs.params.$el.offsetTop)
      this.themeTopYs.push(this.$refs.comment.$el.offsetTop)
      this.themeTopYs.push(this.$refs.recommend.$el.offsetTop)
      this.themeTopYs.push(Number.MAX_VALUE)

      // console.log(this.themeTopYs)
    },
    titleClick(index) {
      // navbar根据点击滚动到对应位置
      this.$refs.scroll.scrollTo(0, -this.themeTopYs[index], 500)
    },
    scrollPosition(position) {
      const positionY = -position.y
      const length = this.themeTopYs.length
      // positionY 与 themeTopYs各个组件顶部偏移量做对比，每次滚动都进行区间判断
      // [0, 2930, 4873, 5059]
      // 当index = 0 , positionY 在 0 和 2930 之间
      // 当index = 1 , positionY 在 2930 和 4873 之间
      // 当index = 2 , positionY 在 4873 和 5059 之间
      // 当index = 3 , positionY 超过 5059

      for (let i = 0; i < length; i++) {
        // 当Y值在每个区间内,遍历作比较
        if (this.currentIndex !== i && (positionY >= this.themeTopYs[i] && positionY < this.themeTopYs[i + 1])) {
          this.currentIndex = i
          this.$refs.navbar.currentIndex = this.currentIndex
        }
      }
      // 判断BackTop是否显示
      this.isShowBackTop = (-position.y) > 800
    },
    addToCart() {
      // 1.创建对象
      const obj = {}
      // 2.对象信息
      obj.iid = this.iid;
      obj.imgURL = this.topImages[0]
      obj.title = this.goods.title
      obj.desc = this.goods.desc;
      obj.newPrice = this.goods.nowPrice;
      // 3.添加到Store中
      this.$store.dispatch('addCart', obj).then(res => {
        this.isShowAlter = true
        this.alterMessage = res

        setTimeout(() => {
          this.isShowAlter = false
          this.alterMessage = ''
        }, 1500)
      })
    }
  }
}
</script>

<style scoped>
#detail {
  position: relative;
  z-index: 9;
  height: 100vh;
}

.content {
  /*给scroll父元素一个高度*/
  height: calc(100% - (44px + 49px));
}

.detail-nav {
  position: relative;
  z-index: 9;
  background-color: #fff;
}


</style>
